
require('../../assets/fonts/iconfont.css')
require('./home.less')

// 引入文档加载事件，dom选择器封装
let {$,$$,$$$} = require('../../utils/dom')

//引入swiper
const {Swiper } = require('swiper/bundle');    
require('swiper/css/bundle')


//引入axios
const axios = require('../../utils/axios')

//引入底部导航
const nav = require('../../utils/nav')
nav.showNav('home');

/**
 * dom文档加载事件
 */
$(function (){

    /**
     * 获取元素
     */
    const rbox = $$('#rbox')
    const clockday = $$('#clockday')
    const clockBtn = $$('#clockBtn')
    const badgenum = $$('#badgenum')
    /**
     * swiper轮播图
     */
     var swiper = new Swiper(".mySwiper", {
        spaceBetween: 30,
        centeredSlides: true,
        autoplay: {
          delay: 2500,
          disableOnInteraction: false,
        },
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
        },
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
      });

    /**
     * 获取用户信息
     */
    axios.get('/api/user/info').then(function (res){
        if( res.data.errno === 0 ){
            //渲染数据
            rbox.textContent = res.data.data.ranking
            clockday.textContent = res.data.data.clockCount
            badgenum.textContent = res.data.data.badges
        };
    })


    /**
     * 今日打卡
     */
    clockBtn.addEventListener('click', function (){
        
        axios.post('/api/user/clockIn').then(function (res){
            if(res.data.errno === 0){
                clockday.textContent = +clockday.textContent + 1
                clockBtn.textContent = '今日已打卡'
            }else{
                alert(res.data.message)
            };
        })
    })

    /**
     * 获取用户打卡信息
     */
    axios.get('/api/user/clockInInfo').then(function (res){
        if( res.data.errno === 0){
           clockBtn.textContent = res.data.data.isClockIn ?  '今日已打卡' : '今日打卡'
        }
    })
    /**
     * lesson跳转
     */
    $$("#lesson").addEventListener("click",function () {
      location.href = "./curriculum.html"
    })
    /**
     * running跳转
     */
    $$("#running").addEventListener("click",function () {
      location.href = "./running.html"
    })
})


